/****************************************Copyright (c)****************************************************
 
**--------------File Info---------------------------------------------------------------------------------
** File name:                main.c
** Last modified Date:       2024.08.07
** Last Version:             Ver:1.0
** Descriptions:             Morse code 正弦波音频发生器
**                        
**--------------------------------------------------------------------------------------------------------
** Created by:               adm
** Created date:             2024.08.07
** Version:                  Ver:1.0
** Descriptions:             
**--------------------------------------------------------------------------------------------------------*/

/*
Copyright (c) [2024]     [BA6IV  ]   [SINEWAVE] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan
PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

#include "at32f415_wk_config.h"

char ssd1306_str[32] = {0};

const unsigned char gImage_img[832] = { /* 0X00,0X01,0X64,0X00,0X40,0X00, */
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X08,0X00,
0XFF,0XFF,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X3E,0X0F,0X80,0X00,0X7E,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X3F,0X38,0X00,0X00,0X07,0XC0,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X7F,0XE0,0X00,0X00,0X00,0X70,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X7F,0X80,0X00,0X00,0X00,0X1C,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X3F,
0X00,0X00,0X00,0X00,0X0E,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X3C,0X00,0X00,0X00,
0X00,0X0F,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X18,0X00,0X00,0X00,0X00,0X0F,0XC0,
0X00,0X00,0X00,0X00,0X00,0X00,0X10,0X00,0X00,0X00,0X00,0X0F,0XF0,0X00,0X00,0X00,
0X00,0X00,0X00,0X30,0X00,0X00,0X00,0X00,0X0F,0XFF,0X00,0X00,0X1E,0X00,0X00,0X00,
0X20,0X00,0X00,0X00,0X00,0X0F,0XFF,0XFE,0X00,0X7F,0X00,0X00,0X00,0X40,0X00,0X00,
0X00,0X00,0X0F,0XFF,0X83,0XE1,0XFF,0X80,0X00,0X00,0XC0,0X00,0X00,0X00,0X00,0X1F,
0XFF,0X00,0X3F,0XFF,0X80,0X00,0X00,0X80,0X00,0X00,0X00,0X00,0X1F,0XFE,0X00,0X07,
0XFF,0X80,0X00,0X01,0X80,0X00,0X00,0X00,0X00,0X3F,0XFC,0X00,0X00,0XFF,0XC0,0X00,
0X01,0X00,0X00,0X00,0X00,0X00,0X7F,0XF8,0X00,0X00,0X7F,0X80,0X00,0X03,0X00,0X00,
0X00,0X00,0X00,0XFF,0XFB,0XFF,0X00,0X1F,0X80,0X00,0X02,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XC0,0X0F,0X80,0X00,0X02,0X00,0X00,0X00,0X00,0X01,0XFF,0XFF,0XFF,
0XE0,0X07,0X80,0X00,0X06,0X00,0X00,0X00,0X00,0X03,0XFF,0XFF,0XFF,0XE0,0X03,0X80,
0X00,0X04,0X00,0X00,0X00,0X00,0X03,0XFF,0XFF,0XFF,0XE0,0X01,0X80,0X00,0X04,0X00,
0X00,0X00,0X00,0X07,0XFF,0XDF,0XFF,0XE0,0X00,0XC0,0X00,0X0C,0X00,0X00,0X00,0X00,
0X07,0XFF,0XCF,0XFF,0XE0,0X40,0X40,0X00,0X08,0X00,0XE0,0X00,0X00,0X0F,0XFF,0X8F,
0XFF,0XE1,0XF0,0X20,0X00,0X18,0X0F,0XFC,0X00,0X00,0X0F,0XFF,0X8F,0XFF,0XC7,0XF8,
0X30,0X00,0X10,0X3F,0XFE,0X00,0X00,0X1F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X00,0X00,0X3F,0XE0,
0X00,0X00,0X3F,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X0F,
0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XFE,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};


const unsigned char gImage_mao[512] = { /* 0X00,0X01,0X40,0X00,0X40,0X00, */
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X0F,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X1F,0X80,0X00,0X00,0X00,0X0F,0X00,0X00,0X3F,0X80,0X00,0X00,0X00,0X3F,0X80,
0X00,0X3F,0XC0,0X7F,0XFF,0XF0,0X7F,0X80,0X00,0X3F,0XCF,0XFB,0XFD,0XFF,0XFF,0X80,
0X00,0X3F,0XFB,0XF3,0XFD,0XF9,0XFF,0X80,0X00,0X3F,0XE1,0XF3,0XFD,0XF8,0X7F,0X80,
0X00,0X3F,0X01,0XF3,0XF9,0XF0,0X1F,0X80,0X00,0X3E,0X01,0XF3,0XF9,0XF0,0X0F,0X00,
0X00,0X18,0X01,0XF3,0XF9,0XE0,0X06,0X00,0X00,0X30,0X00,0XF3,0XF9,0XE0,0X03,0X00,
0X00,0X60,0X00,0XF3,0XF9,0XC0,0X01,0X80,0X00,0XC0,0X00,0XF3,0XF9,0XC0,0X00,0X80,
0X01,0X80,0X00,0X73,0XF1,0X80,0X00,0XC0,0X03,0X00,0X00,0X33,0XF0,0X00,0X00,0X60,
0X02,0X00,0X00,0X03,0XF0,0X00,0X00,0X20,0X06,0X00,0X00,0X01,0XF0,0X00,0X00,0X30,
0X04,0X00,0X00,0X01,0XE0,0X00,0X00,0X30,0X0C,0X00,0X00,0X01,0XE0,0X00,0X00,0X18,
0X18,0X00,0X07,0X00,0XE0,0X1C,0X00,0X18,0X10,0X00,0X0F,0X80,0X00,0X3E,0X00,0X08,
0X10,0X00,0X18,0XC0,0X00,0X63,0X00,0X0C,0X38,0X00,0X10,0XC0,0X00,0X41,0X00,0X04,
0X3C,0X00,0X30,0XC0,0X00,0X41,0X00,0X04,0X3F,0X00,0X00,0X00,0X00,0X00,0X00,0X1E,
0X7F,0X80,0X00,0X00,0X00,0X00,0X00,0X7E,0X7F,0XE0,0X00,0X01,0XF8,0X00,0X00,0XFE,
0X7F,0XF0,0X00,0X03,0XFC,0X00,0X01,0XFE,0X4F,0XF8,0X00,0X07,0XFE,0X00,0X03,0XFE,
0X43,0XF0,0X00,0X07,0XFF,0X00,0X03,0XFF,0X60,0X00,0X00,0X0F,0XFF,0X00,0X01,0XE3,
0XF8,0X00,0X00,0X0F,0XFF,0X00,0X00,0X07,0XFF,0X00,0X00,0X0F,0XFF,0X00,0X00,0X1F,
0XFF,0XE0,0X00,0X0F,0XFF,0X00,0X00,0XFF,0XFF,0XF8,0X00,0X01,0XF0,0X00,0X03,0XFF,
0XFF,0XFC,0X00,0X00,0X60,0X00,0X0F,0XFF,0XFF,0XFE,0X0C,0X00,0X60,0X00,0X1F,0XFF,
0XFF,0XFC,0X1E,0X00,0X60,0X00,0X3F,0XFB,0XC0,0XE0,0X3F,0X00,0X60,0X0E,0X1F,0X83,
0X40,0X00,0X1F,0X00,0X60,0X1F,0X00,0X03,0X70,0X00,0X1E,0X00,0X60,0X1F,0X00,0X1F,
0X7F,0XE0,0X0C,0X00,0X60,0X1F,0X01,0XFE,0X7F,0XF0,0X00,0X00,0X60,0X0E,0X01,0XFE,
0X7F,0XE0,0X00,0X00,0X60,0X00,0X01,0XFE,0X3F,0XC0,0X00,0X00,0X60,0X00,0X00,0X7E,
0X3E,0X00,0X00,0X00,0X60,0X00,0X00,0X04,0X30,0X00,0X00,0X00,0X60,0X00,0X00,0X04,
0X10,0X00,0X00,0X00,0XE0,0X00,0X00,0X0C,0X18,0X00,0X00,0X10,0XE1,0X80,0X00,0X08,
0X0C,0X00,0X00,0X19,0XB1,0X00,0X00,0X18,0X04,0X00,0X00,0X0F,0X1B,0X00,0X00,0X18,
0X06,0X00,0X00,0X00,0X0E,0X00,0X00,0X30,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X20,
0X01,0X80,0X00,0X00,0X00,0X00,0X00,0X60,0X00,0XC0,0X00,0X00,0X00,0X00,0X00,0XC0,
0X00,0X60,0X00,0X00,0X00,0X00,0X03,0X80,0X00,0X38,0X00,0X00,0X00,0X00,0X06,0X00,
0X00,0X1E,0X00,0X00,0X00,0X00,0X1C,0X00,0X00,0X03,0XC0,0X00,0X00,0X00,0X70,0X00,
0X00,0X00,0XFC,0X00,0X00,0X03,0XC0,0X00,0X00,0X00,0X0F,0XF8,0X00,0X7F,0X00,0X00,
0X00,0X00,0X00,0X7F,0XFF,0XF0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};


/**
  * @brief main function.
  * @param  none
  * @retval none
  */
int main(void)
{
    static uint8_t old_di = 0, old_da = 0;
    
    /* 系统初始化 */
    wk_system_clock_config();   //系统时钟配置
    wk_periph_clock_config();   //外设时钟配置
    wk_debug_config();          //dubug接口配置
    wk_tmr11_init();
    wk_nvic_config();           //系统中断配置
    
    /* 外设初始化 */
    Sinewave_Init();            //正弦波初始化
    
    /* I2C 初始化 */
//    /* init dma1 channel2 */
//    wk_dma1_channel2_init();
//    /* config dma channel transfer parameter */
//    /* user need to modify define values DMAx_CHANNELy_XXX_BASE_ADDR and DMAx_CHANNELy_BUFFER_SIZE in at32xxx_wk_config.h */
//    wk_dma_channel_config(DMA1_CHANNEL2, (uint32_t)&I2C1->dt, DMA1_CHANNEL2_MEMORY_BASE_ADDR, DMA1_CHANNEL2_BUFFER_SIZE);
//    dma_channel_enable(DMA1_CHANNEL2, TRUE);

//    /* init dma1 channel3 */
//    wk_dma1_channel3_init();
//    /* config dma channel transfer parameter */
//    /* user need to modify define values DMAx_CHANNELy_XXX_BASE_ADDR and DMAx_CHANNELy_BUFFER_SIZE in at32xxx_wk_config.h */
//    wk_dma_channel_config(DMA1_CHANNEL3, (uint32_t)&I2C1->dt, DMA1_CHANNEL3_MEMORY_BASE_ADDR, DMA1_CHANNEL3_BUFFER_SIZE);
//    dma_channel_enable(DMA1_CHANNEL3, TRUE);

//    /* init i2c1 function. */
    wk_i2c1_init();

    /* 电键初始化 */
    MyKey.KeyInit();
    
    /* 用户按钮初始化 */
    user_button_init();
    
    /* SSD1306 OLED Init */
    ssd1306_Init();
    
    delay_ms(5);
    
    /* OLED演示 */
//    ssd1306_TestAll();

    ssd1306_Fill(Black);                    //清屏
    ssd1306_DrawBitmap(10, 0, gImage_img, 104, 64, White);
    ssd1306_UpdateScreen();
    delay_ms(3000);
    
    ssd1306_Fill(Black);                    //清屏
    ssd1306_DrawBitmap(32, 0, gImage_mao, 64, 64, White);
    ssd1306_UpdateScreen();
    delay_ms(3000);

    ssd1306_Fill(Black);                    //清屏
    ssd1306_DrawRectangle(0, 0, 127, 63, White);
    
    //显示电键类型
    if(MyKey.KEY_Mode == straight) {
        ssd1306_FillRectangle(3, 3, 59, 14, White);
        ssd1306_SetCursor(4, 5);
        ssd1306_WriteString("Straight", Font_7x10, Black);  
    }
    else {
        ssd1306_FillRectangle(3, 3, 46, 14, White);
        ssd1306_SetCursor(4, 5);
        ssd1306_WriteString("Paddle", Font_7x10, Black);
    }
    
    //显示电键音调值
    ssd1306_FillRectangle(88, 3, 124, 14, White);
    ssd1306_SetCursor(89, 5);
    sprintf(ssd1306_str, "%dHz", MyKey.tone);
    ssd1306_WriteString(ssd1306_str, Font_7x10, Black);
    
    ssd1306_UpdateScreen();
    
    /* 启动FreeRTOS任务调度器 */
//    vTaskStartScheduler();      //启动任务调度器后，要注意延时函数要使用操作系统提供的
    
    while (1) {
        
        if(MyKey.KeyEnable_Flag == TRUE) KeyProcessorFun();     //电键处理函数
        
        /* 按键处理 */
        if(button2_LONG_PRESS_Flag == SET) {
            button2_LONG_PRESS_Flag = RESET;
            Di_num(1);
            delay_ms(500);
            while(1) {
                if(button1_SINGLE_Click_Flag == SET) {
                    button1_SINGLE_Click_Flag = RESET;
                    if(MyKey.tone >= 350) MyKey.tone = MyKey.tone - 50;
                    sin_data_init(MyKey.tone);
                    sin_data_scale_set(MyKey.tone_volume);
                    Da();Da();Da();Char_Space();Da();Di();Da();
                }
                
                if(button3_SINGLE_Click_Flag == SET) {
                    button3_SINGLE_Click_Flag = RESET;
                    if(MyKey.tone < 1000) MyKey.tone = MyKey.tone + 50;
                    sin_data_init(MyKey.tone);
                    sin_data_scale_set(MyKey.tone_volume);
                    Da();Da();Da();Char_Space();Da();Di();Da();
                }
                
                if(button2_SINGLE_Click_Flag == SET) {
                    button2_SINGLE_Click_Flag = RESET;
                    Di_num(2);
                    while(1) {
                        if(button1_SINGLE_Click_Flag == SET) {
                            button1_SINGLE_Click_Flag = RESET;
                            if(MyKey.tone_volume >= 5) MyKey.tone_volume = MyKey.tone_volume - 5;
                            sin_data_init(MyKey.tone);
                            sin_data_scale_set(MyKey.tone_volume);
                            Da();Da();Da();Char_Space();Da();Di();Da();
                        }
                        
                        if(button3_SINGLE_Click_Flag == SET) {
                            button3_SINGLE_Click_Flag = RESET;
                            if(MyKey.tone_volume <= 95) MyKey.tone_volume = MyKey.tone_volume + 5;
                            sin_data_init(MyKey.tone);
                            sin_data_scale_set(MyKey.tone_volume);
                            Da();Da();Da();Char_Space();Da();Di();Da();
                        }
                        if(button2_LONG_PRESS_Flag == SET) {
                            button2_LONG_PRESS_Flag = RESET;
                            Da();Da();Da();
                            button2_LONG_PRESS_Flag = SET;
                            break;
                        }
                    }
                }

                if(button2_LONG_PRESS_Flag == SET) {
                    button2_LONG_PRESS_Flag = RESET;
                    Da();Da();Da();
                    break;
                }
            }
        }
        
        //电键图形显示
        if(old_di != MyKey.di_flag) {
            if(MyKey.di_flag == SET) {
                ssd1306_FillRectangle(20, 20, 40, 40, White);
                ssd1306_UpdateScreen();
                
                }
            
            else {
                ssd1306_FillRectangle(20, 20, 40, 40, Black);
                ssd1306_UpdateScreen();
            }
            old_di = MyKey.di_flag;
        }
        
        if(old_da != MyKey.da_flag) {
            if(MyKey.da_flag == SET) {
                ssd1306_FillRectangle(80, 20, 100, 40, White);
                ssd1306_UpdateScreen();
            }
            else {
                ssd1306_FillRectangle(80, 20, 100, 40, Black);
                ssd1306_UpdateScreen();
            }
            old_da = MyKey.da_flag;
        }
        
    }
}
